// components/my-cartBody/my-cartBody.js
import { createStoreBindings, storeBindingsBehavior,observer} from 'mobx-miniprogram-bindings'
import {store} from '../../store/store'
import eventBus from '../../eventBus';
Component({
  behaviors:[storeBindingsBehavior],
  storeBindings:{
    store,
    fields:{
      // Totalprice:'Totalprice',
      // isSelect:"isSelect"
    },
    actions:{
      // addCartPrice:'addCartPrice'
    }
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },
  
  
  /**
   * 组件的初始数据
   */
  data: {
    lis:[
      {id:0,address:"西一自取",src:"/images/cat.jpg",price:50,goodsName:"商品0",goodsNum:1,isSelect:0},
      {id:1,address:"西一自取",src:"/images/cat.jpg",price:50,goodsName:"商品1",goodsNum:1,isSelect:0},
      {id:2,address:"西一自取",src:"/images/cat.jpg",price:50,goodsName:"商品2",goodsNum:1,isSelect:0},
      {id:3,address:"西一自取",src:"/images/cat.jpg",price:50,goodsName:"商品3",goodsNum:1,isSelect:0},
      {id:4,address:"西一自取",src:"/images/cat.jpg",price:50,goodsName:"商品4",goodsNum:1,isSelect:0},
      {id:5,address:"西一自取",src:"/images/cat.jpg",price:50,goodsName:"商品5",goodsNum:1,isSelect:0},
    ],
    selectLists:[],
    isChange:0,
    totalPrice:0,
    isSelectListsChanging:0,
    isUse:false
  },
  observers:{
    'lis':function(){
      this.setData({
        totalPrice:0,
        selectLists:[]
      })
      this.data.lis.map(item=>{
        if(item.isSelect!=0){
          this.setData({
            totalPrice:item.price*item.goodsNum+this.data.totalPrice,
            isSelectListsChanging:0
          })
          var selectLists=this.data.selectLists
          selectLists.push(item)
          this.setData({
            selectLists:selectLists
          })
          console.log('selectLists is changing');
          console.log(this.data.selectLists);
        }else{
          this.setData({
            isSelectListsChanging:1
          })
        }
      })
      // this.addCartPrice(this.data.totalPrice)
    },
    'selectLists':function(){
        if(this.data.lis.length===this.data.selectLists.length){
          eventBus.emit('numEnough',1)
        }else{
          eventBus.emit('numEnough',0)
        }
          var packagea=JSON.stringify(this.data.selectLists)
          eventBus.emit('transmitToBottom',packagea)
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    selectGood(e){
      console.log(e.target.dataset.id);
      var id=e.target.dataset.id
      // 选中
      let newList = this.data.lis.map(item => {
        if (item.id === id && this.data.lis[id].isSelect===0) {
          console.log(this.data.totalPrice);
          return { ...item, isSelect:1}; // 修改对应id的元素的isSelect属性
        }
        else if (item.id === id && this.data.lis[id].isSelect===1){
          
          console.log(this.data.totalPrice);
          return { ...item, isSelect:0};
        }
        return item;
      });  
      this.setData({
        lis: newList
      });
      
    },
    decreaseNum(e){
      var id=e.target.dataset.id
      // 选中
      let newList = this.data.lis.map(item => {
        if (item.id === id) {
          if(item.goodsNum>1){
            
            console.log(this.data.totalPrice);
            return { ...item, goodsNum:item.goodsNum-1}; // 修改对应id的元素的isSelect属性
          }
         
        }
        return item;
      });  
      this.setData({
        lis: newList
      });
    },
    addNum(e){
      var id=e.target.dataset.id
      // 选中
      let newList = this.data.lis.map(item => {
        if (item.id === id) {
          if(item.goodsNum>=0){
            return { ...item, goodsNum:item.goodsNum+1}; // 修改对应id的元素的isSelect属性
          }
        }
        return item;
      });  
      this.setData({
        lis: newList
      });
    }
  },
  lifetimes:{
  attached(){
    // 没问题
    eventBus.on('updateData', () => {
      // 遍历对象数组并修改部分数据
      this.setData({
        isUse:!this.data.isUse
      })
      const updatedData = this.data.lis.map(item => {
       if(this.data.isUse){
        item.isSelect=1
       }else{
        item.isSelect=0
       }
        return item;
      });
      this.setData({
        lis: updatedData
      });
      
    });
    eventBus.on('changeToEdit',(data)=>{
      if(data){
        var packagea=JSON.stringify(this.data.selectLists)
        eventBus.emit('transmitToBottom',packagea)
      }
    });
    eventBus.on('timeToDelete',()=>{
      let newList = this.data.lis.filter(item => item.isSelect!==1);  
      this.setData({
        lis: newList
      });
    })
  },
  detached() {
    eventBus.off('updateData');
    eventBus.off('changeToEdit');
    eventBus.off('timeToDelete')
  }
}

})
